package cn.itcast.backend2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
public class RoleController {
    @Autowired
    StringRedisTemplate redisTemplate;

    @Role()
    @PostMapping("/api/login")
    public String login(@RequestParam String username, @RequestParam String password, Integer role, HttpServletRequest request, HttpSession session) {
        if ("admin".equals(username) && "admin123".equals(password)) {
            session.setAttribute("role", Role.ADMIN);
//            return ResponseEntity.ok().build();
            return "管理的登录成功";
        } else if ("user".equals(username) && "user123".equals(password)) {
            session.setAttribute("role", Role.USER);
//            return ResponseEntity.ok().build();
            return "用户登录成功";
        } else {
            session.setAttribute("role", Role.GUEST);
            return "游客登录成功";
        }
    }

    @Role(value = Role.USER)
    @GetMapping("/api/user")
    public ResponseEntity<Void> getUser(@RequestParam String id, HttpServletRequest request, HttpServletResponse response) {
        return ResponseEntity.ok().build();
    }

    @Role(value = Role.ADMIN)
    @DeleteMapping("/api/userdel")
    public ResponseEntity<Void> deleteUser(@RequestParam String id, HttpServletRequest request, HttpServletResponse response) {
        return ResponseEntity.ok().build();
    }
}

